import 'dart:io';

import 'package:dio/dio.dart';
import 'package:fwanandroid/model/bean/ApiResponse.dart';

class RequestInterceptors extends InterceptorsWrapper {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    ApiResponse apiResponse = ApiResponse.createApiResponse(response);
    if ((response.statusCode == 200 || response.statusCode == 201) &&
        apiResponse.errorCode == 200) {
      handler.next(response);
    } else {
      // 业务异常
      handler.reject(
          DioException(
              requestOptions: response.requestOptions,
              response: response,
              type: DioExceptionType.badResponse,
              error: HttpException(apiResponse.errorMsg ?? "未知异常")),
          true);
    }
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    super.onError(err, handler);
  }
}
